//
//  SettingsViewController.m
//  Wtracker
//
//  Created by Anna on 6/29/10.
//  Copyright 2010 __MyCompanyName__. All rights reserved.
//

#import "SettingsViewController.h"
#import "WtrackerAppDelegate.h"
#import "detailViewController.h"
#import "EntryViewController.h"
#import "addressBookViewController.h"


#define BARBUTTON(TITLE, SELECTOR) [[[UIBarButtonItem alloc] initWithTitle:TITLE style:UIBarButtonItemStylePlain target:self action:SELECTOR] autorelease]

#define UOMTITLE @"Unit of Measure"


@implementation SettingsViewController

@synthesize myDict, sectionArray, sectionTitleArray, aTableView, selectedUnit, unitArray;

#pragma mark -
#pragma mark View lifecycle


- (void)viewDidLoad {
    [super viewDidLoad];
	NSLog(@"in svc");
	
	WtrackerAppDelegate *appDelegate = [[UIApplication sharedApplication] delegate];
	[appDelegate loadMySettings];
	myDict = appDelegate.myDict;

	self.navigationItem.title = @"Settings";
	self.navigationItem.rightBarButtonItem = BARBUTTON(@"Save", @selector(saveSettings));
	NSLog(@"myDict: %@", myDict);
	[self setupArrays];
	[aTableView reloadData];
}

- (void)viewDidAppear:(BOOL)animated{
    [super viewDidAppear:animated];
	NSLog(@"in view did appear");
	// this is for coming back from addressvc, to update all settings, etc.
	
	WtrackerAppDelegate *appDelegate = [[UIApplication sharedApplication] delegate];
	[appDelegate loadMySettings];
	myDict = appDelegate.myDict;
	NSLog(@"after going to delegate and reassigning, myDIct: %@", myDict);
	
	[self setupArrays];
	[aTableView reloadData];
}

#pragma mark -
#pragma mark Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
	return [sectionTitleArray count];
}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
	if(section < 3){
		return 1;
	} else {
		return 2;
	}
}


- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
	return [sectionTitleArray objectAtIndex:section];

}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    static NSString *CellIdentifier = @"Cell";
	NSString *theUOMStr= [myDict objectForKey:UOMTITLE];

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] retain];
    }

	// setup the accessory type

	if(indexPath.section > 2){
		NSString *cellValue = [[sectionArray objectAtIndex:indexPath.section] objectAtIndex:indexPath.row];
		cell.textLabel.text = cellValue;
		// show the right checkmark (accessorytype) to reflect myDict preferences

				
		NSInteger uomIndex = [unitArray  indexOfObject:theUOMStr];		
		if( uomIndex == indexPath.row ){
			cell.accessoryType = UITableViewCellAccessoryCheckmark;
		} else {
			cell.accessoryType = UITableViewCellAccessoryNone;
		}

	} else {
		NSString *cellVal = [sectionArray objectAtIndex:indexPath.section];
		cell.textLabel.text = cellVal;
	}
	return cell;
}



#pragma mark -
#pragma mark Table view delegate

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {

	NSString *theUOMStr = [myDict objectForKey:UOMTITLE];
	// flip back and forth like a radio button

	if( indexPath.section > 2 ){
		[tableView deselectRowAtIndexPath:indexPath animated:NO];
		
		// check with state to see if already selected		
		NSInteger myUOMRow = [unitArray indexOfObject:theUOMStr];

		if (myUOMRow == indexPath.row) {
			return;
		}
		NSIndexPath *oldIndexPath = [NSIndexPath indexPathForRow:myUOMRow inSection:indexPath.section];
		
		UITableViewCell *newCell = [tableView cellForRowAtIndexPath:indexPath];
		UITableViewCell *oldCell = [tableView cellForRowAtIndexPath:oldIndexPath];
		
		if (newCell.accessoryType == UITableViewCellAccessoryNone) {
			newCell.accessoryType = UITableViewCellAccessoryCheckmark;
			[myDict setObject:newCell.textLabel.text forKey:UOMTITLE];
		}		
		
		if (oldCell.accessoryType == UITableViewCellAccessoryCheckmark) {
			oldCell.accessoryType = UITableViewCellAccessoryNone;
		}					
	} else {
		UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
		NSString *cellStr = cell.textLabel.text;
		
		if(indexPath.section == 2){
			addressBookViewController *avc = [[addressBookViewController alloc] initWithNibName:@"addressBookViewController" bundle:nil];
			[[self navigationController] pushViewController:avc animated:YES];
			[avc release];
			
		} else {
			detailViewController *dvc = [[detailViewController alloc] initWithNibName:@"detailViewController" bundle:nil];
			dvc.cellStr = cellStr;
			dvc.sectionTitle = [sectionTitleArray objectAtIndex:indexPath.section];
			[[self navigationController] pushViewController:dvc animated:YES];
			[dvc release];
		}
	}
}

#pragma custom methods


-(void)setupArrays{
	NSLog(@"in setupArrays");
	sectionTitleArray = [[NSMutableArray alloc] initWithObjects:@"Your Name", @"Your Email", @"Preferred default email", @"Preferred Unit of Measure", nil];

	// create a nice looking array for tableview
	unitArray = [[[NSArray alloc] initWithObjects: @"lbs",@"kgs", nil] retain];	
	sectionArray = [[NSMutableArray alloc] initWithObjects:
					[myDict objectForKey:@"Your Name"],
					[myDict objectForKey:@"Your Email"],
					[myDict objectForKey:@"Preferred default email"],
					unitArray,
					nil];
}

-(IBAction)saveSettings{
	[myDict setValue:[NSNumber numberWithInt:1] forKey:@"Started" ];

	WtrackerAppDelegate *appDelegate = [[UIApplication sharedApplication] delegate];
	appDelegate.myDict = myDict;

	[appDelegate saveSettings];

	NSLog(@"in save settings");
	NSLog(@"myDict: %@", myDict);

	[self.navigationController popViewControllerAnimated:YES];
}

#pragma mark -
#pragma mark Memory management

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    
}


- (void)viewDidUnload {
}


- (void)dealloc {
	
    [super dealloc];
}


@end

